package com.mbigger.admin.common;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import com.mbigger.admin.entity.User;

import java.io.IOException;

@WebFilter(urlPatterns = "/*", filterName = "userFilter")
public class UserFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;

        User user = (User)req.getSession().getAttribute("user");
        if (user != null) {
        	RequestHolder.add(user);
            RequestHolder.add(req);
        }
        
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }
}
